package edu.maping.easy.painter.napplet;

import processing.core.PApplet;
import edu.maping.easy.ControlFacade;
import edu.maping.easy.gui.ButtonList;
import edu.maping.easy.gui.GUIEvent;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.gui.Slider;
import edu.maping.easy.model.Thing;
import edu.maping.easy.painter.EventPainter;
import edu.maping.easy.painter.ThingPainterDelegate;
import edu.maping.easy.painter.UtilPainter;
import edu.maping.easy.painter.plugins.RectangleTransformEMNApplet;
import edu.maping.easy.sequence.ActionConf;
import napplet.EMNapplet;

public abstract class EMNAppletAdapter extends EMNapplet implements
		ThingPainterDelegate {

	protected UtilPainter utilPainter = new UtilPainter();

	public Thing thing = null;

	@Override
	public void setThing(Thing thing) {
		this.thing = thing;

	}

	@Override
	public void setConf(ActionConf actionConf) {
		for (int i = 0; i < utilPainter.size(); i++) {
			Slider slider = (Slider) utilPainter.get(i);
			String key = slider.name;
			if (actionConf.containsKey(key)) {
				float value = actionConf.get(key);
				setParameters(key, value);
				utilPainter.setParameter(key, value);
			} else {
				utilPainter.setParameter(key, 0);
			}
		}

	}

	@Override
	public EventPainter controlEvent(GUIEvent theEvent) {
		EventPainter eventPainter = utilPainter.controlEvent(theEvent);
		if (eventPainter != null)
			setParameters(eventPainter.name, eventPainter.value);
		return eventPainter;
	}

	protected abstract void setParameters(String name, float value);

	public abstract String getName();

	@Override
	public void showForm(int x, int y) {
		utilPainter.setPosition(x, y);
		utilPainter.render();
	}

	@Override
	public void hideForm() {
		utilPainter.hide();
	}

	@Override
	public void setup(PApplet pApplet, ControlFacade controlFacade,
			ButtonList buttonList, GuiFactory guiFactory) {

		utilPainter.setup(pApplet, controlFacade, buttonList, guiFactory);

	}

	public ActionConf createNewActionConf() {
		return utilPainter.createActionConf();
	}

	@Override
	public abstract ThingPainterDelegate createNewInstance();
}
